using System;
using System.Collections.Generic;
using System.Text;

namespace Instedd.Labs.Geo.Library.Kml.Styles
{
    internal static class StylesUtil
    {
        public static Uri PostOfficeMessageImageUri = new Uri(@"http://maps.google.com/mapfiles/kml/shapes/post_office.png");

        public const string MessageMinutesAgoCategory1Style = "MessageMinutesAgoCategory1Style";
        public const string MessageMinutesAgoCategory2Style = "MessageMinutesAgoCategory2Style";
        public const string MessageMinutesAgoCategory3Style = "MessageMinutesAgoCategory3Style";
        public const string MessageMinutesAgoCategory4Style = "MessageMinutesAgoCategory4Style";

        public const string PathStyle = "PathStyle";

        public static string GenerateStyleUrl(string styleId)
        {
            if (!styleId.StartsWith("#"))
            {
                return string.Concat("#", styleId);
            }

            return styleId;
        }

        public static Style ApplyPathStyle(KmlFormat KmlFormat)
        {
            Style pathStyle = new Style(PathStyle);
            pathStyle.LineStyle = new LineStyle();
            KmlFormat.Styles.Add(pathStyle);
            return pathStyle;
        }

        public static void ApplyPlacemarkTransparencyStyle(KmlFormat KmlFormat)
        {
            KmlFormat.Styles.Add(CreatePlacemarkTransparencyStyle("NormalMessageMinutesAgoCategory1Style", 100));
            KmlFormat.Styles.Add(CreatePlacemarkTransparencyStyle("NormalMessageMinutesAgoCategory2Style", 60));
            KmlFormat.Styles.Add(CreatePlacemarkTransparencyStyle("NormalMessageMinutesAgoCategory3Style", 40));
            KmlFormat.Styles.Add(CreatePlacemarkTransparencyStyle("NormalMessageMinutesAgoCategory4Style", 20));
            KmlFormat.Styles.Add(CreatePlacemarkTransparencyStyle("HighlightMessageStyle", 100));

            KmlFormat.StyleMaps.Add(CreateStyleMap(MessageMinutesAgoCategory1Style, "NormalMessageMinutesAgoCategory1Style", "HighlightMessageStyle"));
            KmlFormat.StyleMaps.Add(CreateStyleMap(MessageMinutesAgoCategory2Style, "NormalMessageMinutesAgoCategory2Style", "HighlightMessageStyle"));
            KmlFormat.StyleMaps.Add(CreateStyleMap(MessageMinutesAgoCategory3Style, "NormalMessageMinutesAgoCategory3Style", "HighlightMessageStyle"));
            KmlFormat.StyleMaps.Add(CreateStyleMap(MessageMinutesAgoCategory4Style, "NormalMessageMinutesAgoCategory4Style", "HighlightMessageStyle"));
        }

        private static Style CreatePlacemarkTransparencyStyle(string id, double opacity)
        {
            Style style = new Style(id);
            style.IconStyle = new IconStyle(Color.White.SetOpacity(opacity), PostOfficeMessageImageUri);
            style.LabelStyle = new LabelStyle(Color.White.SetOpacity(opacity));
            return style;
        }

        private static StyleMap CreateStyleMap(string id, string normalStyleId, string highlightStyleId)
        {
            StyleMap styleMap = new StyleMap(id);
            styleMap.NormalStyleUrl = GenerateStyleUrl(normalStyleId);
            styleMap.HighlightStyleUrl = GenerateStyleUrl(highlightStyleId);
            return styleMap;
        }
    }
}
